/*
 * gurl_conf.c
 * This file is part of gURL
 *
 * Copyright (C) 2014 - Aleksey Konovalov
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Library General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include "gurl_conf.h"

#include <glib/gi18n.h>
#include <stdio.h>

static const gchar* config_file_name = "gurl.conf";
static GString* config_file_path = NULL;
static GKeyFile* key_file = NULL;

void config_open() {
    if(!config_file_path) {
        config_file_path = g_string_new(g_get_user_config_dir());
        g_string_append_c(config_file_path, '/');
        g_string_append(config_file_path, config_file_name);
    }
    if(!key_file)
        key_file = g_key_file_new();
    if(!g_key_file_load_from_file(key_file, config_file_path->str,
                                  G_KEY_FILE_NONE, NULL))
        g_warning(_("Can't load configuration file \"%s\""),
                  config_file_path->str);
}

void config_close() {
    if(config_file_name && key_file) {
        FILE* f = fopen(config_file_path->str, "wb");
        if(!f)
            g_error(_("Can't open configuration file \"%s\" for writing"),
                    config_file_path->str);
        else {
            gsize len = 0;
            gchar* data = g_key_file_to_data(key_file, &len, NULL);
            if(data)
                fwrite(data, len, sizeof(gchar), f);
            fclose(f);
        }
    }
    if(config_file_path) {
        g_string_free(config_file_path, TRUE);
        config_file_path = NULL;
    }
    if(key_file) {
        g_key_file_free(key_file);
        key_file = NULL;
    }
}

gint config_get_int(const gchar* group, const gchar* key, gint defval) {
    if(!key_file)
        return defval;
    GError* error = NULL;
    int val = g_key_file_get_integer(key_file, group, key, &error);
    return error ? defval : val;
}

void config_set_int(const gchar* group, const gchar* key, gint val) {
    if(key_file)
        g_key_file_set_integer(key_file, group, key, val);
}

gboolean config_get_bool(const gchar* group, const gchar* key, gboolean defval) {
    if(!key_file)
        return defval;
    GError* error = NULL;
    int val = g_key_file_get_boolean(key_file, group, key, &error);
    return error ? defval : val;
}

void config_set_bool(const char* group, const gchar* key, gboolean val) {
    if(key_file)
        g_key_file_set_boolean(key_file, group, key, val);
}

gchar* config_get_str(const gchar* group, const gchar* key, const gchar* defval) {
    if(!key_file)
        return g_strdup(defval);
    gchar* res = g_key_file_get_string(key_file, group, key, NULL);
    return res ? res : g_strdup(defval);
}

void config_set_str(const gchar* group, const gchar* key, const gchar* val) {
    if(key_file)
        g_key_file_set_string(key_file, group, key, val);
}

gchar** config_get_str_list(const gchar* group, const gchar* key,
                            gchar** defval) {
    if(!key_file)
        return g_strdupv(defval);
    gsize len;
    gchar** res = g_key_file_get_string_list(key_file, group, key, &len, NULL);
    return res ? res : g_strdupv(defval);
}

void config_set_str_list(const gchar* group, const gchar* key,
                         const gchar** value, gsize len) {
    if(key_file)
        g_key_file_set_string_list(key_file, group, key, value, len);
}
